package edu.cuit.avatar.collection;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Stack;

/**
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/14 16:46
 *
 */
public class StackTest {
    public static void main(String[] args) {
        var stack = new Stack<String>();
        System.out.println(stack.isEmpty()); //同步方法
        stack.push("Linux");
        stack.push("Oracle");
        stack.push("java");
        stack.push("java web");
        stack.push("SSM");

        System.out.println(stack.search("java"));//true
        System.out.println(stack.search("javaME"));//false

        while(!stack.isEmpty()){
            System.out.println(stack.pop());
        }

        System.out.println(stack.isEmpty()); //同步方法

        System.out.println(predicate("D:\\workspace\\cuit-046\\javase-20250714\\src\\main\\java\\edu\\cuit\\avatar\\collection\\Geometric.java"));
    }


    public static boolean predicate(String java){
        try(var input = new BufferedReader(new FileReader(java))){
            String line = null;
            Stack<Character> stack = new Stack<>();
            while((line = input.readLine()) != null){
                for(var i = 0; i < line.length(); i++){
                    if(line.charAt(i) == '(' || line.charAt(i) == '{' || line.charAt(i) == '[')
                        stack.push(line.charAt(i));
                    if(line.charAt(i) == ')' && stack.pop() != '(')
                        return false;
                    if(line.charAt(i) == ']' && stack.pop() != '[')
                        return false;
                    if(line.charAt(i) == '}' && stack.pop() != '{')
                        return false;
                }
            }
            return true;
        } catch (IOException e){
            e.printStackTrace();
            return false;
        }
    }
}
